import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.xml.stream.XMLStreamException;

public class Server {

	private static networkHandler net;
	private static boolean autopilot;

	public static void main(String[] argv) throws XMLStreamException,
			IOException, InterruptedException {

		autopilot = false;
		String robotName;
		Map map = new Map();
		JFrame frame = new JFrame();
		frame.add(map, BorderLayout.CENTER);
		frame.setSize(map.map_size_height, map.map_size_width);
		frame.setVisible(true);

		while (true) {

			net = new networkHandler(4444);
			net.connect();

			String robotname;
			String nameBuffer;
			while (true) {
				if ((nameBuffer = net.read()).startsWith("robotname:")) {
					robotname = nameBuffer.substring(10);
					System.out.println("Will connect to robot: " + robotname);
					break;
				}
			}

			while (net.isConnected()) {

				ClientNetworkInput cnet = new ClientNetworkInput(net);
				cnet.start();
				imageSender imSender = new imageSender(net, map);
				imSender.start();

				RobotInterface rif = new RobotInterface(robotname);

				while (rif.isConnected()) {
					// Autopilot should be evoked from the client
					// TODO: method for that

					if (autopilot) {
						int numScans = 60;
						Vector<Integer> scanResult = new Vector<Integer>();
						rif.addScan(numScans);
						try {
							rif.send();
							Thread.sleep(200);
							rif.recieve();
						} catch (Exception e) {
							rif.clearInput();
							// } catch (XMLStreamException e) {
							// e.printStackTrace();
						}
						System.out.println("Kristian!: "
								+ rif.getInXMLHandler().toString("r2s"));

						for (Integer i = 0; i < rif.getInXMLHandler()
								.getCommand().size(); i++) {
							System.out.println("int " + i.toString() + ": "
									+ rif.getInXMLHandler().getName(i));
						}

						for (int i = 1; i <= numScans; i++) {
							scanResult.add(rif.getInXMLHandler().getValue(1,
									"n" + i));
						}
						System.out.println(scanResult.toString());
						int max = 0;
						int index_max = 0;
						for (int it = 0; it < numScans; it++) {
							if (scanResult.elementAt(it) > max) {
								max = scanResult.elementAt(it);
								index_max = it;
							}
						}
						System.out.println("index_max: " + index_max);
						int temp = (360 / numScans) * index_max;
						map.Update(rif.getInXMLHandler());
						rif.clear();
						rif.addRotate(temp);
						rif.addMoveDistance(max);
						// try {
						rif.send();
						rif.recieve();
						// } catch (IOException e) {
						// e.getMessage();
						// } catch (XMLStreamException e) {
						// // TODO Auto-generated catch block
						// e.printStackTrace();
						// }
						map.Update(rif.getInXMLHandler());

						frame.add(map, BorderLayout.CENTER);
						frame.setSize(map.map_size_height, map.map_size_width);
						frame.setVisible(true);
						frame.repaint();
						rif.clear();
					}

					if (!cnet.returnCommand().isEmpty()) {
						System.out.println(cnet.returnCommand().toString(null));
						System.out.println(cnet.returnCommand().isAuto(0));
						if (cnet.returnCommand().isAuto(0)) {
							autopilot = !autopilot;
						} else {
							rif.send(cnet.returnCommand().toString("s2r"));
							rif.recieve();
							map.Update(rif.getInXMLHandler());
							rif.clearInput();
						}
						cnet.returnCommand().clear();
					}
				}

				// int numScans = 1;
				// Vector<Integer> scanResult = new Vector<Integer>();
				// rif.addScan(numScans);
				// rif.send();
				// rif.recieve();
				// System.out.println("Kristian!: " +
				// rif.getInXMLHandler().toString("r2s"));
				// for(int i = 0; i < numScans; i++){
				// scanResult.add(rif.getInXMLHandler().getValue(1, "n"+i));
				// }
				// System.out.println(scanResult.toString());
				// map.Update(rif.getInXMLHandler());
				// rif.clear();
				// Random rand = new Random();
				// int x = rand.nextInt(1);
				// int rotate;
				// if(x==0)
				// rotate = -90;
				// else
				// rotate = 90;
				// rif.addRotate(rotate);
				// rif.addMoveDistance(400);
				// rif.send();
				// rif.recieve();
				// map.Update(rif.getInXMLHandler());
				// frame.add(map, BorderLayout.CENTER);
				// frame.setSize(map.map_size_height, map.map_size_width);
				// frame.setVisible(true);
				// frame.repaint();
				// rif.clear();

				// }

			}
		}

	}
}

// }
